package cn.iocoder.yudao.module.travel.service.travel1;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.travel.controller.admin.travel1.vo.*;
import cn.iocoder.yudao.module.travel.dal.dataobject.travel1.TravelDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.travel.dal.mysql.travel1.TravelMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.travel.enums.ErrorCodeConstants.*;

/**
 * 用户基本信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class TravelServiceImpl implements TravelService {

    @Resource
    private TravelMapper travelMapper;

    @Override
    public Long createTravel(TravelSaveReqVO createReqVO) {
        // 插入
        TravelDO travel = BeanUtils.toBean(createReqVO, TravelDO.class);
        travelMapper.insert(travel);
        // 返回
        return travel.getUserId();
    }

    @Override
    public void updateTravel(TravelSaveReqVO updateReqVO) {
        // 校验存在
        validateTravelExists(updateReqVO.getUserId());
        // 更新
        TravelDO updateObj = BeanUtils.toBean(updateReqVO, TravelDO.class);
        travelMapper.updateById(updateObj);
    }

    @Override
    public void deleteTravel(Long id) {
        // 校验存在
        validateTravelExists(id);
        // 删除
        travelMapper.deleteById(id);
    }

    private void validateTravelExists(Long id) {
        if (travelMapper.selectById(id) == null) {
            throw exception(TRAVEL_NOT_EXISTS);
        }
    }

    @Override
    public TravelDO getTravel(Long id) {
        return travelMapper.selectById(id);
    }

    @Override
    public PageResult<TravelDO> getTravelPage(TravelPageReqVO pageReqVO) {
        return travelMapper.selectPage(pageReqVO);
    }

}